import Vue from 'vue';
import Router from 'vue-router';
import Cookies from 'js-cookie';
import Layout from '@/layout';

Vue.use(Router);

export const routes = [{
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },{
    path: '/factory_view',
    component: () => import('@/views/niuwan/factory_view'),
    hidden: true,
    meta: {
      title: '牛丸文创'
    }
  }, 
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [{
      path: 'home',
      name: '首页',
      component: () => import('@/views/home/home'),
      meta: {
        title: '首页'
      }
    }]
  }, 
  {
    path: '/admin',
    component: Layout,
    name: '管理员功能',
    meta: {
      title: '管理员功能'
    },
    children: [{
      path: 'MenuList',
      name: '菜单管理',
      component: () => import('@/views/admin/MenuList'),
      meta: {
        title: '菜单管理'
      }
    }, {
      path: 'rolelist',
      name: '角色管理',
      component: () => import('@/views/admin/rolelist'),
      meta: {
        title: '角色管理'
      }
    }, {
      path: 'staffManagement',
      name: '员工管理',
      component: () => import('@/views/admin/staffManagement'),
      meta: {
        title: '员工管理'
      }
    }]
  }, 
  {
    path: '/book',
    component: Layout,
    name: '图书管理',
    meta: {
      title: '图书管理'
    },
    children: [
      {
        path: 'my_book',
        name: '我的书',
        component: () => import('@/views/book/my_book'),
        meta: {
          title: '我的书'
        }
      },
      {
        path: 'author',
        name: '作者管理',
        component: () => import('@/views/book/author'),
        meta: {
          title: '作者管理'
        }
      },
       {
        path: 'type',
        name: '类型管理',
        component: () => import('@/views/book/type'),
        meta: {
          title: '类型管理'
        }
      },{
        path: 'press',
        name: '出版社管理',
        component: () => import('@/views/book/press'),
        meta: {
          title: '出版社管理'
        }
      },
    ]
  },
  {
    path: '/niuwan',
    component: Layout,
    name: '牛丸文创',
    meta: {
      title: '牛丸文创'
    },
    children: [
      {
        path: 'type',
        name: '功能管理',
        component: () => import('@/views/niuwan/type'),
        meta: {
          title: '功能管理'
        }
      },{
        path: 'image_type',
        name: '图样类型管理',
        component: () => import('@/views/niuwan/image_type'),
        meta: {
          title: '图样类型管理'
        }
      },
      {
        path: 'image',
        name: '图样管理',
        component: () => import('@/views/niuwan/image'),
        meta: {
          title: '图样管理'
        }
      },
       {
        path: 'material',
        name: '物料管理',
        component: () => import('@/views/niuwan/material'),
        meta: {
          title: '物料管理'
        }
      },{
        path: 'activity',
        name: '活动管理',
        component: () => import('@/views/niuwan/activity'),
        meta: {
          title: '活动管理'
        }
      },{
        path: 'factory_order',
        name: '工厂补货',
        component: () => import('@/views/niuwan/factory_order'),
        meta: {
          title: '工厂补货'
        }
      },{
        path: 'statistics',
        name: '统计',
        component: () => import('@/views/niuwan/statistics'),
        meta: {
          title: '统计'
        }
      }
    ]
  },
  {
    path: '/consignment',
    component: Layout,
    name: '合作',
    meta: {
      title: '合作'
    },
    children: [
      {
        path: 'consignment_shop',
        name: '合作店铺',
        component: () => import('@/views/consignment/consignment_shop'),
        meta: {
          title: '合作店铺'
        }
      },
      {
        path: 'consignment',
        name: '寄售管理',
        component: () => import('@/views/consignment/consignment'),
        meta: {
          title: '寄售管理'
        }
      }
    ]
  }
]


const createRouter = () => new Router({
  scrollBehavior: () => ({
    y: 0
  }),
  routes
});
const router = createRouter();
router.beforeEach((to, from, next) => {
  if (to.path !== '/login' && to.path != '/factory_view') {
    if (Cookies.get('crmtoken') === undefined) {
      router.push({
        path: "/login"
      })
    } else {
      next();
    }
  } else {
    next()
  }
});

export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
